package cn.Liujialin.pet.controller;


import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.LoginContext;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.pet.domain.Pet;
import cn.Liujialin.pet.domain.SearchMasterMsg;
import cn.Liujialin.pet.query.SearchMasterMsgQuery;
import cn.Liujialin.pet.service.ISearchMasterMsgService;
import cn.Liujialin.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/seachMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findByAll();
    }

    /*
    * x发布寻找主人的消息 前端主站页面*/
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            //获取当前登录人的工具,LoginContext这是我们自己导入的工具类，不是自带的那个工具类
            LoginInfo loginInfo = LoginContext.getCurrentLoginInfo(request);
            seachMasterMsgService.publish(searchMasterMsg,loginInfo.getId());
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙，稍后重试!");
        }

    }
    //处理寻主消息

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet,LoginContext.getCurrentLoginInfo(request));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统异常！");
        }
    }



    //查询个人消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.queryUserMsg(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //查询已处理消息  state=1和shopId 来区分平台或者店铺
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }
    //待处理和已经处理的消息差别在state是否为O上面
    //查询待处理消息   state=0和shopId 来区分平台或者店铺
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.pending(query,LoginContext.getCurrentLoginInfo(request));
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }







}
